/*
*Copyright (c) Nikhil Jain.
*Project ICARUS, communications and 
*artificial intelligence.
*/



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package javadrone.command;

//~--- JDK imports ------------------------------------------------------------

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author Nikzzay
 */
public abstract class SynchronousCommand implements Command {
    private long     registerTimeMillis;
    private TimeUnit sourceUnit;
    private long     timeout;

    public SynchronousCommand(long timeout, TimeUnit sourceUnit) {
        this.timeout            = timeout;
        this.sourceUnit         = sourceUnit;
        this.registerTimeMillis = System.currentTimeMillis();
    }

    @Override
    public long getDelay(TimeUnit unit) {
        long currentTimeMillis = System.currentTimeMillis();

        return unit.convert(TimeUnit.MILLISECONDS.convert(timeout, sourceUnit)
                            - (currentTimeMillis - registerTimeMillis), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed D) {
        return (int) (getDelay(TimeUnit.MILLISECONDS) - D.getDelay(TimeUnit.MILLISECONDS));
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
